/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.common')
        .service('AppUtils', AppUtils);

    /**
     * 常用逻辑方法
     */
    function AppUtils($ionicPopup,$ionicLoading,$ionicScrollDelegate,$window) {

        return {
            isUndefinedOrNull: function(obj) {
                if(angular.isArray(obj)){
                    return obj==null||obj.length==0 ? true : false;
                }
                return !angular.isDefined(obj) || obj===null || obj=='';
            },
            showAlert: function(msg){
                $ionicPopup.alert({
                    title: '提示',
                    template: msg
                });
            },
            showConfirm: function(msg){
                var confirmPopup = $ionicPopup.confirm({
                    title: '提示',
                    template: msg
                });
                return confirmPopup;
            },
            loading: function(){
                $ionicLoading.show({
                    content: 'Loading',
                    animation: 'fade-in',
                    showBackdrop: true,
                    maxWidth: 200,
                    showDelay: 0
                });
            },
            loadfinish: function(){
                $ionicLoading.hide();
            },
            scrollTop: function(){
                $ionicScrollDelegate.scrollTop();
            },
            goBack: function(){
                $window.history.go(-1);
            },
            showTipsTop: function(msg){
                layer.open({
                    content: msg,
                    //anim: 'top',
                    skin: 'top',
                    time: 1000
                });
            },
            getClientType: function(){
                var userAgent = navigator.userAgent || navigator.vendor || window.opera;
                // Windows Phone must come first because its UA also contains "Android"
                if (/windows phone/i.test(userAgent)) {
                    return "wp";
                }
                if (/android/i.test(userAgent)) {
                    return "android";
                }
                // iOS detection from: http://stackoverflow.com/a/9039885/177710
                if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
                    return "ios";
                }
                return "unknown";
            }
        }
    }
})();